let addr=require('../model/address.js')

async function create(data){
   let {user_id,consignee,phone,address,is_default}=data
   let res=await addr.create({user_id,consignee,phone,address,is_default})
   return res.dataValues
}

async function findAddress(id){
    let res=await addr.findAll({
        where:{
            user_id:id
        },
        attributes:['id', 'consignee', 'phone', 'address', 'is_default']
    })
    return res
}

async function update(data){
    let res=await addr.update({consignee:data.consignee,phone:data.phone,address:data.address,is_default:data.is_default},
        {where:{
            user_id:data.user_id,
            id:data.id
        }})
    return res[0]>1?true:false
}

async function remove(id){
    let res=await addr.destroy({where:{
        id
    }})
    console.log(res,'删除地址')
    return res
}

module.exports={
    create,
    findAddress,
    update,
    remove
}